package org.gary.equipment.dao.impl;

import java.sql.SQLException;

import javax.annotation.Resource;

import org.gary.equipment.dao.EquipmentDao;
import org.gary.equipment.model.Equipment;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

/**
 * 
 * @author lengreen
 *
 */
@Component("equipmentDao")
public class EquipmentDaoImpl implements EquipmentDao {
	private HibernateTemplate hibernateTemplate;
	private String hql;
	
	@Override
	public <T> T loadImages(int equipmentId) {
		setHql("select equipment.images from " + Equipment.class.getName()
				+ " equipment where equipment.id=" + equipmentId);
		T t = hibernateTemplate.execute(new HibernateCallback<T>() {

			@SuppressWarnings("unchecked")
			@Override
			public T doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query q = session.createQuery(getHql());
				return (T) q.list();
			}
		});
		return t;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}

	public String getHql() {
		return hql;
	}

}
